package com.workingpub.commons.jdbc.config.internal;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;

import javax.xml.transform.stream.StreamSource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.workingpub.commons.jdbc.JaxbResolver;
import com.workingpub.commons.jdbc.SqlDefineContext;
import com.workingpub.commons.jdbc.binding.SelectEntry;
import com.workingpub.commons.jdbc.binding.SqlDefineEntry;
import com.workingpub.commons.jdbc.binding.SqlMapping;
import com.workingpub.commons.jdbc.config.SqlDefineResolver;
import com.workingpub.commons.jdbc.exception.JdbcAccessException;

@Service("sqlDefineResolver")
public class DefauleSqlDefineResolver implements SqlDefineResolver {
	private static final Logger logger = LoggerFactory.getLogger(DefauleSqlDefineResolver.class);

	@Autowired
	private SqlDefineContext sqlDefineContext;

	@Override
	public void addSqlDefine(File defineFile) {
		SqlDefineEntry sqlDefineEntry = resolve(defineFile);
		sqlDefineContext.addSqlDefines(sqlDefineEntry);

	}

	@Override
	public void addSqlDefine(InputStream defineStream) {
		SqlDefineEntry sqlDefineEntry = resolve(defineStream);
		sqlDefineContext.addSqlDefines(sqlDefineEntry);
	}

	@Override
	public void addQuerySql(String sqlId, String sqlText) {
		SqlMapping sqlMapping = resolveQuerySql(sqlId, sqlText);
		sqlDefineContext.addSqlMapping(sqlMapping, SqlDefineContext.Default_Domain_Group);
	}

	@Override
	public void addQuerySql(String sqlId, String sqlText, String domainCode) {
		if (StringUtils.isBlank(domainCode)) {
			throw new JdbcAccessException("The parameter domainCode is null or empty.");
		}
		SqlMapping sqlMapping = resolveQuerySql(sqlId, sqlText);
		sqlDefineContext.addSqlMapping(sqlMapping, domainCode);
	}

	@Override
	public void removeDefine(InputStream defineStream) {
		SqlDefineEntry sqlDefineEntry = resolve(defineStream);
		sqlDefineContext.removeSqlDefines(sqlDefineEntry);

	}

	@Override
	public void removeDefine(File defineFile) {
		SqlDefineEntry sqlDefineEntry = resolve(defineFile);
		sqlDefineContext.removeSqlDefines(sqlDefineEntry);
	}

	@Override
	public SqlDefineEntry resolve(File defineFile) {
		if (null == defineFile) {
			throw new JdbcAccessException(
					String.format("Input sql define file is null or not exists : %s", defineFile));
		}
		SqlDefineEntry defineEntry = null;
		try {
			defineEntry = resolve(new BufferedInputStream(new FileInputStream(defineFile)));
		} catch (FileNotFoundException e) {
			Assert.isNull(e, e.getMessage());
		}
		return defineEntry;
	}

	@Override
	public SqlDefineEntry resolve(InputStream defineStream) {
		if (null == defineStream) {
			throw new JdbcAccessException("The input define stream is null.");
		}
		try {
			return JaxbResolver.getInstance().unmarshal(new StreamSource(defineStream));
		} finally {
			try {
				defineStream.close();
			} catch (IOException e) {
				logger.warn("Close input stream error : " + e.getMessage());
			}
		}
	}

	@Override
	public SqlDefineEntry resolve(String defineText) {
		if (StringUtils.isNotBlank(defineText)) {
			throw new JdbcAccessException("The input define text is null or empty.");
		}
		return JaxbResolver.getInstance().unmarshal(new StreamSource(new StringReader(defineText)));
	}

	@Override
	public SelectEntry resolveQuerySql(String sqlId, String sqlText) {
		if (StringUtils.isBlank(sqlId) || StringUtils.isBlank(sqlText)) {
			throw new JdbcAccessException("The input define text is null or empty.");
		}
		return JaxbResolver.getInstance().unmarshalQuerySql(sqlId, sqlText);
	}
}
